package mainpackage;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.CharBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParseHtml {

	String fileName;
	String document;
	public ParseHtml(String fileName) throws Exception
	{
		this.fileName = fileName;
		document = new String(Files.readAllBytes(Paths.get(fileName)));
		document = RemoveTags(document);
	}
	String RemoveTags(String html){
		   html = html.replaceAll("</p>", "\n");
		   html = html.replaceAll("<div>", "__div__");
		   html = html.replaceAll("\\<.*?>","");
		   html = html.replaceAll("&nbsp;","");
		   html = html.replaceAll("&amp;","");
		   return html;
		}
	public ArrayList<ArticleData> GetData()
	{
		System.out.println("ddd");
		ArrayList<ArticleData> data = new ArrayList<ArticleData>();
		Pattern pattern = Pattern.compile("Document [\\d]+ of 1000");
		Pattern pattern2 = Pattern.compile("Author:");
		Pattern pattern3 = Pattern.compile("Full text:");
		Pattern pattern4 = Pattern.compile("Section:([^\n]*)");
		Matcher matcher = pattern.matcher(document);
		while(matcher.find())
		{
			String content = document.substring(matcher.end());
			content = content.trim();
			String title = content.substring(0, content.indexOf('\n'));
			System.out.println(content.substring(0,50));
			System.out.println(title);
			content = content.substring(content.indexOf('\n') + 1);
			Matcher matcher2 = pattern2.matcher(content);
			matcher2.find();
			content = content.substring(matcher2.end());
			String author = content.substring(0, content.indexOf('\n'));
			System.out.println("author:"+author);
			content.trim();
			Matcher matcher3 = pattern3.matcher(content);
			matcher3.find();
			//System.out.println(content);
			int endIndex = Math.min(content.indexOf("__div__"), content.indexOf("Subject:"));
			int compIndex = content.indexOf("Company / organization:");
			int pubTitle = content.indexOf("Publication title:");
			int authorAffiliation = content.indexOf("AuthorAffiliation");
			if(compIndex > 0)
			endIndex = Math.min(endIndex,  compIndex);
			if(pubTitle > 0)
				endIndex = Math.min(endIndex,  pubTitle);
			if(authorAffiliation>0)
				endIndex = Math.min(endIndex, authorAffiliation);
			String fullText = content.substring(matcher3.end(), endIndex);
			fullText = fullText.trim();
			System.out.println("Full text:"+fullText);
			
			content = content.substring(endIndex);
			Matcher matcher4 = pattern4.matcher(content);
			matcher4.find();
			String section = matcher4.group(1).trim();
			//String section = content.substring(content.indexOf("Section:"));
			ArticleData articleData = new ArticleData(title, author, fullText, section);
			data.add(articleData);
			
		}
		return data;
	}
}
